import java.util.*;
import java.util.function.Consumer;

//lambda表达式
@FunctionalInterface
interface NoPragmaterNoReturn{
    void test();
}

@FunctionalInterface
interface PrintFun{
    void test(int x);
}

@FunctionalInterface
interface AddFun{
    int add(int x,int y);
}

public class Lambda {

    public static void main(String[] args) {
       List<String> list = new LinkedList<>();

       list.add("hello");
       list.add("world");
       list.add("everyThink");
       list.add("sometime");

       list.sort((a,b) ->{
           return a.compareTo(b);
       });

        list.forEach(new Consumer<String>(){
            @Override
            public void accept(String s){
                System.out.println(s);
            }
        });
    }
    public static void main3(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return (Integer) o1 - (Integer) o2;
            }
        });

        PriorityQueue<Integer> q = new PriorityQueue<>((a,b) ->{
            return a - b;
        });

        queue.add(1);
        queue.add(4);
        queue.add(3);

        for(Integer x : queue) System.out.println(x + " ");
    }
    public static void main2(String[] args) {
        //匿名内部类：使用AddFun接口，并重新add方法
        AddFun a = new AddFun(){
            @Override
            public int add(int x,int y) {
                return x + y;
            }
        };
        System.out.println(a.add(1,2));
        AddFun b = (x,y) -> {
            return x + y;
        };
        System.out.println(b.add(1,2));

    }

    public static void main1(String[] args) {
        PrintFun p = new PrintFun() {
            @Override
            public void test(int x) {
                System.out.println(x);
            }
        };

        p.test(1);

        PrintFun o = (a) -> {
            System.out.println(a);
        };
        o.test(22);
    }
}
